(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('glSystemService', glSystemService);
	glSystemService.$inject = [ '$q', 'loggerFactory', 'commonService','ajaxServiceFactory' ];
	function glSystemService($q, loggerFactory, commonService,ajaxServiceFactory) {
		var log = loggerFactory.getLogger("glSystemService");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getGlSystemOptions : {
				url : 'do/cda/dataList/glSystemOptions',
				method : 'get'
			},
		    getGlSystemLayout : {
		    	url : 'do/cda/glSystem/layout',
				method : 'post'
		   }
		});
		function loadOptions() {
			
			var defer = $q.defer();
			ajaxService.getGlSystemOptions().then(function(response) {
				var glSystemOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue.data = value;
						glSystemOptions[key] = tempValue;
					});
				}
				defer.resolve(glSystemOptions);
			});
			return defer.promise;
		}

		
		
		function getGlSystemLayout(action) {
			return ajaxService.getGlSystemLayout({
				'action' : action
			});
		}
		function glsystemMgrChanged(ctrl, gridConfigs) {
			 if(gridConfigs){
				  angular.forEach(gridConfigs, function(gridConfig, key) {
					  if(gridConfig.dynamicParameter && ctrl.glSystem.glsystemMgr != gridConfig.dynamicParameter.subCategory){
						  ctrl.layoutConfig.formConfigs.general.gridConfigs[key].active = false;
					  }else  if(gridConfig.dynamicParameter && ctrl.glSystem.glsystemMgr == gridConfig.dynamicParameter.subCategory){
						  ctrl.layoutConfig.formConfigs.general.gridConfigs[key].active = true;
					  }
				  });
			  }
		}
		function initOptions(ctrl, glSystemOptions) {
			ctrl.yesNoOptions = glSystemOptions.yesOrNoOptions;
			ctrl.dataStatusOptions = glSystemOptions.dataStatus.data;
			

		};
			
		function initPageCallback(ctrl) {
			ctrl.glsystemMgrChanged = function() {
				glsystemMgrChanged(ctrl, ctrl.layoutConfig.formConfigs.general.gridConfigs);
			};
		}
		
		return {
			loadOptions : loadOptions,
			initOptions : initOptions,
			getGlSystemLayout : getGlSystemLayout,
			initPageCallback : initPageCallback
		
		};
		
  }	
		
		
})(angular);